﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Paltop_DatabaseLayer;
using System.Net;
using System.IO;
using System.Xml.Linq;

namespace Paltop_.Controllers
{
    public class FeedController
    {
        private PaltopDatabaseEntities db = new PaltopDatabaseEntities();

        public int addField(string title, string url, string desc, int accountID)
        {
            try
            {
                FeedList t = new FeedList { Title = title, link = url, Description = desc, AccountID = accountID };
                db.FeedLists.AddObject(t);
                db.SaveChanges();
                return 0;
            }
            catch (Exception)
            {

                return -1;
            }

        }
        public FeedList RssToObject(string url, int accounTID = 2)
        {
            try
            {
                WebClient client = new WebClient();

                Stream data = client.OpenRead(url);
                StreamReader reader = new StreamReader(data);
                string s = reader.ReadToEnd();
                XDocument feed = XDocument.Parse(s);
                var feeds = (from p in feed.Descendants("channel")
                             select new FeedList
                             {
                                 Title = p.Element("title").Value,
                                 link = p.Element("link").Value,
                                 Description = p.Element("description").Value
                             }).FirstOrDefault();
                feeds.rssLink = url;
                FeedList t = new FeedList();
                t.link = feeds.link;
                t.rssLink = feeds.rssLink;
                t.Title = feeds.Title;
                t.Description = feeds.Description;
                t.AccountID = accounTID;
                var c = db.FeedLists.Where(p => p.AccountID == accounTID && p.rssLink == url).ToList();
                if (c.Count < 1)
                {
                    db.FeedLists.AddObject(t);
                    db.SaveChanges();
                    return feeds;
                }
                else return null;
            }
            catch (Exception)
            {

                return null;
            }

        }
        public List<FeedList> getAccountFeedList(int id)
        {
            try
            {
                var t = db.FeedLists.Where(p => p.AccountID == id).ToList();
                return t;
            }
            catch (Exception)
            {

                throw;
            }

        }

        public List<Paltop_v0._1.Entities.feedItem> getItemWithID(int id)
        {
            try
            {

                string output="";
                var temp= db.FeedLists.Where(p => p.FeedID == id).First();
                string url = temp.rssLink;
                WebClient client = new WebClient();

                Stream data = client.OpenRead(url);
                StreamReader reader = new StreamReader(data);
                string s = reader.ReadToEnd();
                XDocument feed = XDocument.Parse(s);
                var items=feed.Elements("item");
                /*var feeds = (from item in feed.Descendants("item")
                             select ("<a href=" + item.Element("link").Value + ">" + "<h3>"
                             + item.Element("title").Value + "</h3></a>" + item.Element("description").Value)
                             ).ToList();
                foreach (var item in feeds)
                {
                    output = output + item;
                }
                */
                var itemss = (from p in feed.Descendants("item")
                             select new Paltop_v0._1.Entities.feedItem
                             {
                                 title = p.Element("title").Value,
                                 link = p.Element("link").Value,
                                 body = p.Element("description").Value,
                                 date = p.Element("pubDate").Value,


                             }).ToList();
 
                return itemss;
            }
            catch (Exception)
            {

                return null;
            }
        }


    }
}